package comunication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import model.Game;
import model.GameException;

public class PlayerCommunicator extends Thread
{
	private Socket client;
	private boolean works;
	private BufferedReader in = null;
	private PrintWriter out = null;
	private Game game;
	private int playerNr;
	private String playerName;
		
	public PlayerCommunicator(Socket client, Game game, int playerNr) {
		this.game = game;
		this.playerNr = playerNr;
        this.client = client;
		this.works = true;
		
		try {
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			out = new PrintWriter(client.getOutputStream(), true);
		} 
		catch (IOException e){
			System.out.println("in or out failed");
			System.exit(-1);
		}
	}
	
	public void run() {
		String line = "";
		while(line != null && works){
			try {
				line = in.readLine();
				System.out.println("Player " + playerName + " get line: " + line);
				if (line.startsWith("Name_")) {
                    this.playerName = line.substring(5);
                    out.println("previousPlayers_" + game.introduceNewPlayer(playerName));
				}
				else if (line.startsWith("Send_")) {
					game.displayMessage(line.substring(5));
				}
				else if (line.startsWith("Call_")) {
					if (game.isThisPlayerTurn(playerNr)&& game.isThisMoveAllowed(playerNr, "C")) {
						game.updateCall(playerNr);
					}
				}
				else if (line.startsWith("Bet_")) {
					if (game.isThisPlayerTurn(playerNr)&& game.isThisMoveAllowed(playerNr, "B")) {
						try {
							game.updateBet(playerNr,line.substring(4));
						}
						catch (GameException e) {
							System.out.println("Server folds " + playerName  + " for cheating");
							game.updateFold(playerNr,true);
						}
					}
				}
				else if (line.startsWith("Raise_")){
					if(game.isThisPlayerTurn(playerNr)&& game.isThisMoveAllowed(playerNr, "R")) {
						try {
							game.updateBet(playerNr,line.substring(6));
						}
						catch(GameException e) {
							System.out.println("Server folds "+ playerName  + " for cheating");
							game.updateFold(playerNr,true);
						}
					}
				}
				else if (line.startsWith("Check_")){
					if(game.isThisPlayerTurn(playerNr)&& game.isThisMoveAllowed(playerNr, "H")) {
						game.nextPlayerMove();
					}
				}
				else if (line.startsWith("All_")){
					if(game.isThisPlayerTurn(playerNr) && game.isThisMoveAllowed(playerNr, "A")) {
						game.updateAllin(playerNr);
					}
				}
				else if (line.startsWith("Fold_")){
					if(game.isThisPlayerTurn(playerNr))
						game.updateFold(playerNr,false);
				}
				else if (line.startsWith("Exchange_")){
					if(game.isThisPlayerTurn(playerNr) && game.isThisMoveAllowed(playerNr, "E")) {
						try{
							game.exchangeCards(playerNr,line.substring(9));
						} catch (GameException e){
							try{
								game.exchangeCards(playerNr, "0000");
							} catch (GameException e1){}
						}
					}
				}
				else if (line.startsWith("Close_")){
					game.closeResults(playerNr);
				}
				else if (line.startsWith("End_")) {
					game.end(playerNr);
					works = false;
				}
			}
			catch (IOException e){
				System.out.println("Player Read failed.");
				works = false;
				game.updateFold(playerNr,true);
				//gracz wyrzucany z pczatkiem nastepnego rozegrania
			}
		}
	}
	public boolean isWorking() {
		return works;
	}
	
	public void addNewPlayer(String name){
		out.println("addNewPlayer" + name);
    }
    public void displayMessage(String message){
    	out.println("Chat_" + message);
    }
    public void displayWinner(String message) {
        try {
            sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("	Winner_" + message);
        out.println("Winner_" + message);
    }
    public void displayBigBlind(String name, String bBlind){
    	out.println("BigBlind_" + name + bBlind);
    }
    public void displaySmallBlind(String name,String sBlind){
    	out.println("SmallBlind_" + name + sBlind);
    }
    public void displayCoins(String amount, String name){
    	System.out.println("	sCoins_"+ name + amount);
    	out.println("sCoins_"+ name + amount);
    }
    public void displayBet(String bet, String name){
    	System.out.println("	Bet_" + name + bet);
    	out.println("Bet_" + name + bet);
    }
    public void enableButtons(String buttons){
    	System.out.println("	Enable_"+buttons);
    	out.println("Enable_"+buttons);
    }
    public void displayFold(String name){
    	System.out.println("	dFold_"+ name);
    	out.println("dFold_"+ name);
    }
    public void displayAllin(String name){
    	System.out.println("	dAllin_"+ name);
    	out.println("dAllin_"+ name);
    }
    public void displayStake(String stake){
    	System.out.println("	Stake_" + stake);
    	out.println("Stake_" + stake);
    }
    public void displayRound(String round){
    	System.out.println("	Round_" + round);
    	out.println("Round_" + round);
    }
    public void displayCards(String hand){
    	System.out.println("	Cards_" + hand);
    	out.println("Cards_" + hand);
    }
    public void displayAmountOfExchanged(String name,String amount){
    	System.out.println("	Amount_"+ name + amount);
    	out.println("Amount_"+ name + amount);
    }
    public void deletePlayer(String name){
    	System.out.println("	Delete_" + name);
    	out.println("Delete_" + name);
    }
    public void displayInfo(String info,boolean isEnd) {
    	if (isEnd) out.println("Info_T" + info);
    	else out.println("Info_F" + info);
    }
}
